package in.muton.edushare.pages.common;

import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EntityListing;
import in.muton.panels.paginator.badgehelpers.course.BatchBadgeInterface;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public class BatchPanel extends EdusharePanel {

	private static final long serialVersionUID = -5764270540783634578L;

	public BatchPanel(String id, final Universities univ,
			EduShareBasePage page) {
		super(id, page);
		univModel = new CayenneModel<Universities>(univ);
		init();
	}
	
	public BatchPanel(String id, final Departments dept, EduShareBasePage page){
		super(id, page);
		deptModel = new CayenneModel<Departments>(dept);
		init();
	}
	
	
	private void init(){
		Form<Void> f = new Form<Void>("form");
		add(f);
		BatchBadgeInterface bbi;
		if(univModel != null)
			bbi = new BatchBadgeInterface(univModel.getObject());
		else
			bbi = new BatchBadgeInterface(deptModel.getObject());
		f.add(new Button("button") {
			@Override
			public void onSubmit() {
				PageParameters params = new PageParameters();
				params.put("0", "0");
				if(univModel != null){
					params.put("1", univModel.getObject().getUniqueName());
				}else{
					DeptDataHandler ddh = new DeptDataHandler(deptModel);
					params.put("1", ddh.getOwner().getObject().getUniqueName());
					params.put("2", deptModel.getObject().getDepartmentId().toString());
				}
//				setResponsePage(NewBatch.class, params);
//				setResponsePage(new TextPanel(params, basePage));
			}
			
			@Override
			public boolean isVisible(){
				if(deptModel!=null){
					DeptRoleCheck drc = new DeptRoleCheck(deptModel.getObject(), getLoggedInUser());
					if(drc.isPrimaryAdmin()||drc.isUnivAdmin())
						return true;
				}else{
					UnivRoleCheck urc = new UnivRoleCheck(univModel.getObject(), getLoggedInUser());
					if(urc.isAdmin())
						return true;
				}
				return false;
			}
			private static final long serialVersionUID = -1853817260280015167L;
		});

		final BatchDAO bDAO = new BatchDAO(getORMInterface());

		add(new EntityListing<Batches>("programsLV",
				new InnerList<Batches>() {
					private static final long serialVersionUID = 1L;
					@Override
					public List<IModel<Batches>> getObject() {
						if(univModel != null)
							return new CayenneList<Batches>(bDAO.getAllBatches(univModel
								.getObject())).getObject();
						else
							return new CayenneList<Batches>(bDAO.getAllBatches(deptModel
									.getObject())).getObject();
					}
				},bbi, getBasePage()));		
	}
	
	private IModel<Universities> univModel = null;
	private IModel<Departments> deptModel = null;
}
